#include <func.h>

typedef struct 
{
    pthread_mutex_t mutex;
    pthread_cond_t cond;
} data_t;

void *pthfunc(void *p)
{
    printf("I am child thread, I sleep\n");
    data_t *d = (data_t *)p;

    // 子线程等待小连招
    pthread_mutex_lock(&d->mutex);
    pthread_cond_wait(&d->cond, &d->mutex); 
    pthread_mutex_unlock(&d->mutex);

    printf("I am child thread, I wake up\n");
    pthread_exit(NULL);

}

int main() 
{
    data_t d;
    pthread_t pthid;

    pthread_mutex_init(&d.mutex, NULL);
    pthread_cond_init(&d.cond, NULL);

    pthread_create(&pthid, NULL, pthfunc, (void *)&d);

    usleep(100);
    pthread_cond_signal(&d.cond); // 唤醒等待的子线程

    pthread_join(pthid, NULL);
    
    pthread_mutex_destroy(&d.mutex);
    pthread_cond_destroy(&d.cond);

    return 0;
}
